<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <!-- 数据列表 -->
    <div class="mt10">
      <el-card shadow="never" :body-style="{ padding: '10px' }">
        <div slot="header" class="mb9 f16">
          <span style="font-weight: 600">订单明细资料</span>
        </div>
        <Table :isLoading="isLoading" :tableData="tableData" :tableHeader="tableHeader" stripe :summaryData="summaryData" summary />

        <!-- 分页器 -->
        <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
      </el-card>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, type Ref, computed } from 'vue'
import { reactive, ref } from 'vue'
import OrderSummary from '@/api/GeneralManager/inquire/OrderSummary'
import { TableList, selectList, type TableListType } from '@/plugins/mixin'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'OrderSummaryO'
})
</script>

<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Api from '@/api/GeneralManager/inquire/OrderSummary'
import { onMounted, watch } from 'vue'
import { debounce, throttle } from 'lodash'
// let height = ref(document.documentElement.clientHeight - 330 + 'px')
// window.addEventListener('resize', () => {
//   height.value = document.documentElement.clientHeight - 330 + 'px'
// })

// 初始化加载数据
onMounted(async () => {
  getTableList()
  getSelectList()
})
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入货号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入人员',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '样品类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '季节',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '客户交期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '下单日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '出货日期',
    dateValue: ''
  },
  {
    type: 'checkbox',
    checked: true,
    checkboxLabel: '已完成'
  },
  {
    type: 'checkbox',
    checked: true,
    checkboxLabel: '未完成'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const tableData = ref([])
const imgList = ref<string[]>([])
const isLoading = ref(false)
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已完成')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未完成')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return '2'
  } else if (Audited.checked) {
    return '1'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcOrder/summary/pcOrderDataSummaryAls', {
    flag: cfmFlag(),
    orderEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[1],
    orderStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[0],
    person: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入人员'
    )?.inputValue,
    prodEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '出货日期'
    )?.dateValue?.[1],
    prodStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '出货日期'
    )?.dateValue?.[0],
    sampleType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '样品类型')
      ?.selectValue,
    season: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '季节')
      ?.selectValue,
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入货号'
    )?.inputValue,
    brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    deliveryEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
    )?.dateValue?.[1],
    deliveryStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
    )?.dateValue?.[0],
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  imgList.value = tableDataList.list.map((item: any) => {
    return item.photoPath
  })
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  // currentRow()
  summaryData.value[0].num = tableDataList.orderSum
}, 500)
// const getTableList = debounce(async () => {

//   let { Data, dataList, srcList } = await TableList(formList, Api.getTableList, currentPage.value, pageSize.value) as TableListType
//   tableData.value = dataList
//   total.value = Data.total
//   imgList.value = srcList
//   summaryData.value[0].num = Data.totalNum
// }, 500)

const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
5
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
    getTableList()
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/pcOrder/summary/downloadOrderDataSummaryAls`, {
      flag: cfmFlag(),
      orderEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
      )?.dateValue?.[1],
      orderStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
      )?.dateValue?.[0],
      person: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入人员'
      )?.inputValue,
      prodEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '出货日期'
      )?.dateValue?.[1],
      prodStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '出货日期'
      )?.dateValue?.[0],
      sampleType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '样品类型')
        ?.selectValue,
      season: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '季节')
        ?.selectValue,
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入货号'
      )?.inputValue,
      brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
        ?.selectValue,
      deliveryEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
      )?.dateValue?.[1],
      deliveryStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客户交期'
      )?.dateValue?.[0],
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
      )?.inputValue,
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}

interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 12,
    num: 0
  }
])

const getSelectList = async () => {
  await selectList(formList, Api.getSelectList)
}

const tableHeader = ref([
  {
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '下单日期',
    prop: 'orderDate'
  },
  {
    label: '季节',
    prop: 'seasonType'
  },
  {
    label: '样品类型',
    prop: 'sampleType'
  },
  {
    type: 'img',
    prop: 'photoPath',
    label: '图片',
    fixed: true,
    srcList: imgList
  },
  {
    label: '指令号',
    prop: 'lotNo',
    width: '170'
  },
  {
    label: '客人货号',
    prop: 'custStyle',
    width: '100'
  },
  {
    label: '工厂货号',
    prop: 'style',
    width: '160'
  },
  {
    label: '颜色',
    prop: 'color'
  },
  {
    label: '楦头',
    prop: 'lastNo'
  },
  {
    label: '大底',
    prop: 'moldNo',
    width: '150'
  },
  {
    label: '号码',
    prop: 'sizeNo'
  },
  {
    label: '数量',
    prop: 'orderQty'
  },
  {
    label: '纸板号',
    prop: 'shoeName'
  },
  {
    label: '面衬',
    prop: 'sockNo'
  },
  {
    label: '客户交期',
    prop: 'deliveryDate'
  },
  {
    label: '出货日期',
    prop: 'closeDate'
  }
])
</script>

<style lang="less" scoped>
.btns {
  display: flex;
  justify-content: space-between;

  .bg {
    background-color: #e7e7e7;
    border: 1px solid #e7e7e7;
    color: #000000;
  }
}

// 底部
.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10rem;

  .total {
    // font-size: 14px;
    color: rgba(0, 0, 0, 0.6);
    font-family: PingFangSC-Regular-, PingFangSC-Regular;
  }

  :deep(.el-pagination .el-select .el-input) {
    width: 60rem;
  }

  :deep(.number) {
    border: 1px solid #dcdcdc;
    box-sizing: border-box;
    margin-right: 4rem;
    border-radius: 2rem 2rem 2rem 2rem;
  }

  :deep(.is-active.number) {
    background-color: #f89027 !important;
    color: white;
    border-radius: 2rem 2rem 2rem 2rem;
  }

  .save {
    color: black;
    border: 1px solid #e7e7e7;
    background: #e7e7e7;
  }
}
</style>
